package cn.itcast.hotel.mq;

import cn.itcast.hotel.service.IHotelService;
import constants.MqConstants;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * @Author Azer
 * @LOVE,,,
 * @Date 2021/11/6 20:36
 */
//监听器
@Component
public class HotelListener {
    @Autowired
    private IHotelService hotelService;

    /**
     * 监听酒店新增或修改的业务
     * @param id 酒店id
     * @throws IOException
     */
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = MqConstants.HOTEL_INSERT_QUEUE),
            exchange = @Exchange(name = MqConstants.HOTEL_EXCHANGE,
                    type = ExchangeTypes.TOPIC),
            key = MqConstants.HOTEL_INSERT_KEY
    ))
    public void ListenHotelInsertUpdate(Long id) throws IOException {
        hotelService.insertById(id);
    }

    /**
     * 监听酒店删除的业务
     * @param id 酒店id
     * @throws IOException
     */
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = MqConstants.HOTEL_DELETE_QUEUE),
            exchange = @Exchange(name = MqConstants.HOTEL_EXCHANGE,
            type = ExchangeTypes.TOPIC),
            key = MqConstants.HOTEL_DELETE_KEY
    ))
    public void ListenHotelDelete(Long id) throws IOException {
        hotelService.deleteById(id);
    }
}
